//
//  ShaidanViewController.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/23.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "ShaidanViewController.h"
#import "ShaidanCell.h"
#import "MyshareModel.h"

@interface ShaidanViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSInteger page;
    UILabel *footLable;
}

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ShaidanViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    // Do any additional setup after loading the view.
    [self addSubviews];
    [self requestData];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)addSubviews
{
    self.title = @"我的晒单";
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"bjq"] style:(UIBarButtonItemStylePlain) target:self action:@selector(actionEdit:)];
    self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShaidanCell" bundle:nil] forCellReuseIdentifier:@"ShaidanCell"];
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
}

- (void)requestData
{
    NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, kMyShareList];
    NSDictionary *dict = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"page":[NSNumber numberWithInteger:page]};
    [NetworkHandle getUpDataWithUrl:string class:NSClassFromString(@"MyshareModel") dictionary:dict firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        if (page == 1) {
            [self.dataArray removeAllObjects];
        }
        [self.dataArray addObjectsFromArray:array];
    
        if (self.dataArray.count == 0 && self.tableView.tableFooterView == nil) {
            footLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 30)];
            footLable.text = @"您没有相关订单";
            footLable.textAlignment = NSTextAlignmentCenter;
            self.tableView.tableFooterView = footLable;
        }
        
        if (self.dataArray.count >= 10 && self.tableView.footer == nil) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        }
        
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    }];
    
}

- (void)headerRefresh
{
    page = 1;
    [self requestData];
    
}

- (void)footerRefresh
{
    page++;
    [self requestData];
    
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 自适应高度
    if (self.dataArray.count > 0) {
        return 274;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShaidanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShaidanCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MyshareModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        // /user.do?cmd=delete_shaidan&u=X&shaidan_id=x
        MyshareModel *model = self.dataArray[indexPath.row];
        NSString *string = [NSString stringWithFormat:@"%@%@", kBaseUrl, @"/user.do?cmd=delete_shaidan"];
        NSDictionary *dict = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"shaidan_id":model.shaidan_id};
        [NetworkHandle getInfoWithUrl:string dictionary:dict success:^(NSDictionary *dictionary) {
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationFade)];
            [self.dataArray removeObject:model];
        }];
        
    }];
    action.backgroundColor = kColors(218, 166, 46, 1);
    NSArray *array = [NSArray arrayWithObjects:action, nil];
    return array;
}



#pragma mark -- 按钮点击事件
- (void)actionEdit:(UIBarButtonItem *)rightBarbtn
{
    // 编辑
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
